package com.hdax.search.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CompletableFutureText {
    /**
     * 1、whenComplete
     * 2、exceptionally
     * 3、handle
     */

    public static ExecutorService executorService=Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("start");
//        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//        }, executorService).whenComplete((result,exception)->{
//            System.out.println("异步任务执行完成");
//            System.out.println("结果"+result);
//            System.out.println("异常"+exception);
//        }).exceptionally(throwable -> {
//            //感知异常，处理默认结果
//            int i = 10;
//            return i;
//        });
//        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executorService).handle((resutl,exception)->{
//            if(resutl!=null){
//                return resutl*2;
//            }
//            if(exception!=null){
//                return 0;
//            }
////            return 2;
//        });
//        Void unused = voidCompletableFuture.get();
        System.out.println("end");
    }
}
